#include <iostream>
#include <functional>
#include <cstring>
#include "boost/asio.hpp"




using boost::asio::ip::tcp;




void ClientWriteCallback(const boost::system::error_code &ec,
        std::size_t bytes_transferred,tcp::socket &socket,char *data)
{
    if (ec)
    {
        std::cout<<"ClientWriteCallback error:"<<ec.message()<<std::endl;
        return;
    }
    
    std::cout<<"服务端回复："<<data<<std::endl;
    memset(data,0,sizeof(data));
    std::cout<<">>请输入要发送的数据：";
    std::cin>>data;
    socket.async_write_some(boost::asio::buffer(data,strlen(data)),
        std::bind(ClientWriteCallback,std::placeholders::_1,
            std::placeholders::_2,std::ref(socket),data));
}


void StartClientWrite(char *data,tcp::socket &socket)
{
    memset(data,0,sizeof(data));
    std::cout<<">>请输入要发送的数据：";
    std::cin>>data;
    socket.async_write_some(boost::asio::buffer(data,strlen(data)),
        std::bind(ClientWriteCallback,std::placeholders::_1,
            std::placeholders::_2,std::ref(socket),data));
}

int main()
{
    boost::asio::io_context io_context;
    tcp::socket socket(io_context);
    socket.connect(tcp::endpoint(boost::asio::ip::address::from_string("192.168.248.171"), 8888));
    char data[1024]={0};

    StartClientWrite(data,socket);


    io_context.run();
    return 0;
}